<?php
/**
 * 邮件发送类
 *
 * @file Mail.clas.php
 * @auther liyangguang
 * @date 2011-11-14
 */
require_once (FITWOW_ROOT_PATH.'lib/phpmailer/class.phpmailer.php');

class Mail {
	
	private $mail = null;
    private $mail_queue = null;

    public function __construct()  {
        global $cfg_smtp;
        //设置要发送的邮件
		$this->mail = new PHPMailer(); 
		$this->mail->IsSMTP(); //设置发送邮件的协议：SMTP  
		$this->mail->Host = $cfg_smtp['host']; 
		$this->mail->SMTPAuth = $cfg_smtp['auth']; 
		$this->mail->SMTPSecure = $cfg_smtp['secure'];   
		$this->mail->Port = $cfg_smtp['port'];   
		$this->mail->Username = $cfg_smtp['username'];
		$this->mail->Password = $cfg_smtp['password'];
		$this->mail->SetFrom($cfg_smtp['from_addr'], $cfg_smtp['from_name']);
		$this->mail->CharSet = $cfg_smtp['charset']; 
		$this->mail->WordWrap = $cfg_smtp['wordwrap'];
		$this->mail->SMTPDebug  = $cfg_smtp['debug']; 
  }
	

      /**
       * called by sendMail, 
       * it can send mail by system's sendmail
       * @return boolean
       */
      private function _system_sendmail($to_address, $subject, $body){
        global $cfg_smtp;
        $from_name= '=?UTF-8?B?'.$cfg_smtp['from_name'].'?=';
        $from_addr= $cfg_smtp['from_addr'];
        ini_set('sendmail_from',$from_addr);

        $headers[]="From: \"{$from_name}\" <{$from_addr}>";
        $headers[]='Content-Type: text/html; charset=utf-8';
        $headers[]=("Return-Path: {$from_addr}");
        $headers[]="MIME-Version: 1.0";
        $headers[]="X-Priority: 3";//mail level,normal is 3
        $headers[]="Sender: {$from_addr}";
        return mail($to_address,'=?UTF-8?B?'.base64_encode($subject).'?=', $body, implode("\n", $headers)) ? true : false;
      }

      /**
       * 发送一个邮件的简单接口
       * @param String $to_address 收件人地址
       * @param String $subject 邮件标题
       * @param String $body 邮件内容
       * @return boolean
       */		

      public function sendMail($to_address, $subject, $body) {
        //patch it!
        $result= $this->_system_sendmail($to_address, $subject, $body);
        if(!$result){
          L($to_address.' email send failed. ','error');
          return false;
        }else{
          return true;
        }
      }

      public function sendSmtpMail($to_address, $subject, $body){
        $this->mail->ClearAddresses();
        if(is_array($to_address)){
          foreach($to_address as $addr){
            $this->mail->AddAddress($addr); 
          }
        }else{
          $this->mail->AddAddress($to_address); 
        }
        $this->mail->Subject = $subject; 
        $this->mail->MsgHTML($body);
        return $this->mail->Send();
      }








}
?>
